package org.dromara.system.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.BeanCopyUtils;
import org.dromara.system.mapper.SysLoginLogMapper;
import org.dromara.system.pojo.bo.SysLoginLogBO;
import org.dromara.system.pojo.entity.SysLoginLogDO;
import org.dromara.system.pojo.qo.SysLoginLogQO;
import org.dromara.system.pojo.vo.SysLoginLogVO;
import org.dromara.system.service.SysLoginLogService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统登录日志 服务层实现。
 *
 * @author mkj
 * @since 2024-07-21
 */
@RequiredArgsConstructor
@Service
public class SysLoginLogServiceImpl implements SysLoginLogService {

    private final SysLoginLogMapper sysLoginLogMapper;

    @Override
    public boolean add(SysLoginLogBO sysLoginLogBO) {
        SysLoginLogDO sysLoginLogDO = BeanCopyUtils.copy(sysLoginLogBO, SysLoginLogDO.class);
        return sysLoginLogMapper.insert(sysLoginLogDO) > 0;
    }

    @Override
    public boolean remove(Long id) {
        return sysLoginLogMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(SysLoginLogBO sysLoginLogBO) {
        SysLoginLogDO sysLoginLogDO = BeanCopyUtils.copy(sysLoginLogBO, SysLoginLogDO.class);
        return sysLoginLogMapper.update(sysLoginLogDO) > 0;
    }

    @Override
    public List<SysLoginLogVO> list(SysLoginLogQO sysLoginLogQO) {
        SysLoginLogDO sysLoginLogDO = BeanCopyUtils.copy(sysLoginLogQO, SysLoginLogDO.class);
        QueryWrapper queryWrapper = QueryWrapper.create(sysLoginLogDO);
        return sysLoginLogMapper.selectListByQueryAs(queryWrapper, SysLoginLogVO.class);
    }

    @Override
    public SysLoginLogVO getInfo(Long id) {
        SysLoginLogDO sysLoginLogDO = sysLoginLogMapper.selectOneById(id);
        if (sysLoginLogDO == null) {
            return null;
        }
        return BeanCopyUtils.copy(sysLoginLogDO, SysLoginLogVO.class);
    }

    @Override
    public Page<SysLoginLogVO> page(SysLoginLogQO page) {
        return sysLoginLogMapper.paginateAs(page.getPageNumber(), page.getPageSize(), QueryWrapper.create(page), SysLoginLogVO.class);
    }

}

